home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / window / ultrawin / uwdemo / str_demo.c < prev    next >
Encoding:
Text File  |  1992-02-02  |  19.9 KB  |  487 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* STR_DEMO.C                                                               */
  4. /*                                                                          */
  5. /* We have had several customers experience difficulty understanding the    */
  6. /* wn_gets function.  This is understandable, it is somewhat confusing.     */
  7. /* This demo will help you understand the features, including several new   */
  8. /* powerful features, and implement simpler forms to get basic items        */
  9. /* such as time, date, phone numbers, etc.  The modified wn_gets is fully   */
  10. /* compatible with the previous version so none of your code need change.   */
  11. /* However, a new function called wn_gets_ll (which wn_gets calls)          */
  12. /* has added the capability to uppercase the first char of each word,       */
  13. /* strip the mask completely, only strip the end, or not strip at all.      */
  14. /* It can also get input right-to-left, clear the entry on the first valid  */
  15. /* key, and return when the last character of the input is typed.           */
  16. /* Most importantly, it can allow the input of a string longer than the     */
  17. /* display width of the field and will scroll within this region auto-      */
  18. /* matically!  It can also display arrows to show the user that the string  */
  19. /* has data to the left or right of the current display field.              */
  20. /*                                                                          */
  21. /* Two newer capabilities of wn_gets_ll are the validation function hook,   */
  22. /* which can be called on each valid key pressed, and the gets hook, which  */
  23. /* is called on wn_gets_ll entry, exit, and for every key pressed.  We will */
  24. /* use the validation hook to show how you can do simple date validation,   */
  25. /* and we will use the gets hook to print out the text, mask and template   */
  26. /* strings for each key pressed, as well as make the F1 key perform a clear */
  27. /* on the input.                                                            */
  28. /*                                                                          */
  29. /* Note:  Compile this file with the warning "parameter 'ident' is never    */
  30. /*        used" turned off, as we don't have to actually use each parameter */
  31. /*        included in the validation and gets hook functions.               */
  32. /*                                                                          */
  33. /*------------------------- flags for wn_gets_ll ---------------------------*/
  34. /*                                                                          */
  35. /* G_STRIP          strips the entire mask from the string.                 */
  36. /*                * 12/04/91 = 120491, (432)-555-3421 = 4325553421                    */
  37. /*                                                                          */
  38. /* G_STRIP_END      strips the end of the string only only.                            */
  39. /*                * John Doe______ = John Doe                                                    */
  40. /*                                                                          */
  41. /* G_UP_FST_CHAR    converts the first letter of each word to uppercase.    */
  42. /*                * this is a test = This Is A Test                         */
  43. /*                                                                          */
  44. /* G_ARROW          displays left and right arrows if display width is less */
  45. /*                  than field width. NOTE: This requires an additional     */
  46. /*                  screen/window space before and after the field.         */
  47. /*                                                                          */
  48. /* G_RIGHT_TO_LEFT  gets input from the right side of the string, scooting  */
  49. /*                  characters over to the left for each valid press.       */
  50. /*                                                                          */
  51. /* G_CLEAR_ON_FIRST clears out the entry when the first valid key is        */
  52. /*                  pressed.  This saves a CTRL-Y keystroke to clear the    */
  53. /*                  entry.                                                  */
  54. /*                                                                          */
  55. /* G_EXIT_ON_FILL   causes the routine to exit whenever the last valid key  */
  56. /*                  is pressed.  This saves an extra <Enter> press.         */
  57. /*                                                                          */
  58. /*--------------------------------------------------------------------------*/
  59. /*                                                                          */
  60. /* Understanding these few flags and looking at the below examples make     */
  61. /* a very complex input routine much more managable!                        */
  62. /*                                                                          */
  63. /****************************************************************************/
  64. #include <stdlib.h>
  65. #include "uw.h"
  66. #include "uw_globx.h"
  67. #include "uw_keys.h"
  68.  
  69.  
  70. /*-------------------------- a few global variables ------------------------*/
  71. WINDOW Main_wn;
  72. char name1[32],  name2[32],  name3[32],
  73.          phone1[16], phone2[16], phone3[16],
  74.          date1[16],  date2[16],  date3[16],
  75.          time1[16],  time2[16],  time3[16];
  76.  
  77.  
  78. /*-------------------------- just some prototypes --------------------------*/
  79. void msg_line( char *msg );
  80. int my_gets(char *s, int max_len, int disp_width, int flags, int clear, WINDOW *wnp);
  81. int my_get_phone(char *s, int disp_width, int flags, WINDOW *wnp);
  82. int my_get_date(char *s, int disp_width, int flags, int clear, WINDOW *wnp);
  83. int my_get_time(char *s, int disp_width, int flags, WINDOW *wnp);
  84. int date_vld(char *w, char *t, char *m, int pos, int max_pos, WINDOW *wnp );
  85. int time_vld(char *w, char *t, char *m, int pos, int max_pos, WINDOW *wnp );
  86. int super_hook( char *text, char *template, char *mask, int pos,
  87.                                 int max_pos, int p_flag, EVENT *eventp, WINDOW *wnp );
  88.  
  89.  
  90. /*********/
  91. /* ~main */
  92. /*       ********************************************************************/
  93. /****************************************************************************/
  94. int main()
  95. {
  96.     int i, r;
  97.     WINDOW *wnp;
  98.  
  99.     init_video(80, 25);
  100.     init_clock(0x3333);
  101.     wnp = &Main_wn;
  102.     wn_create( 0, 0, V_cols-1, V_rows-1, DBL_BDR, WN_NORMAL, wnp );
  103.     wn_color( WHITE, BLUE, wnp );
  104.     wn_bdr_color( WHITE, BLUE, wnp );
  105.     wn_set(wnp);
  106.     wnp->inside = OFF;
  107.     wn_plst(CENTERED, 0, "<<< EnQue's String Entry Demonstration >>>", wnp);
  108.     wn_color(LIGHTGRAY, BLUE, wnp);
  109.     wn_plst(CENTERED, 1, "F1 is translated to CTRL-Y and parameters displayed by the new gets hook.", wnp);
  110.  
  111.     /* NOTE:
  112.          When we use set_gets_hook or set_validation_func, we can just call the
  113.          name of the function when using good old "C", but we have to do the
  114.          wild-looking function pointer cast when we use the C++ compiler.
  115.          If you rename this "C" file to a "CPP" file and compile under Turbo or
  116.          Borland C++ you will get a "Type mismatch in parameter func_ptr"
  117.          compiler error (not warning) if you just use the function name without
  118.          the cast!
  119.     */
  120.  
  121. #ifdef __cplusplus
  122.     set_gets_hook( (int (*)(...)) super_hook );
  123. #else
  124.     set_gets_hook( super_hook );
  125. #endif
  126.  
  127.     wn_color( WHITE, BLUE, wnp );
  128.     wnp->inside = ON;
  129.  
  130. /*---------- display full width and convert first char to uppercase --------*/
  131.     r = 2;
  132.     msg_line("Entry with first letters capped and no strip");
  133.     wn_plst(  2, r, "Enter your name  : ", wnp );
  134.     my_gets(name1, 22, 22, G_UP_FST_CHAR, 1, wnp);
  135.     wn_plst( 50, r, name1, wnp );
  136.  
  137. /*----------- display full width with strip to end and exit on fill --------*/
  138.     r++;
  139.     msg_line("Entry with strip to end and exit on fill");
  140.     wn_plst(  2, r, "Enter your name  : ", wnp );
  141.     my_gets(name2, 22, 22, G_STRIP_END | G_EXIT_ON_FILL, 1, wnp);
  142.     wn_plst( 50, r, name2, wnp );
  143.  
  144. /*---------- display only 6 characters, strip the entire mask, -------------*/
  145. /*---------- convert first char, and display arrows...         -------------*/
  146.     r++;
  147.     msg_line("Entry with first letters capped, strip, scrolling, and arrow chars");
  148.     wn_plst(  2, r, "Enter your name  :  ", wnp );
  149.     my_gets(name3, 22, 6, G_STRIP | G_UP_FST_CHAR | G_ARROW, 1, wnp);
  150.     wn_plst( 50, r, name3, wnp );
  151.  
  152. /*---------- display full width, strip the entire mask, --------------------*/
  153. /*---------- convert first char, and clear on first key --------------------*/
  154.     r++;
  155.     msg_line("Entry with first letters capped, strip, and clear on first");
  156.     wn_plst(  2, r, "Enter your name  : ", wnp );
  157.     my_gets(name3, 22, 22, G_STRIP | G_UP_FST_CHAR | G_CLEAR_ON_FIRST, 0, wnp);
  158.     wn_plst( 50, r, name3, wnp );
  159.  
  160. /*--------- display full width, strip to end, enter right-to-left ----------*/
  161.     r += 2;
  162.     msg_line("Right-to-left entry with strip to end");
  163.     wn_plst(  2, r, "Enter your phone # : ", wnp );
  164.     my_get_phone(phone1, 14, G_STRIP_END | G_RIGHT_TO_LEFT, wnp);
  165.     wn_plst( 50, r, phone1, wnp );
  166.  
  167. /*------------------- display full width, don't strip mask -----------------*/
  168. /*------------------- and exit when the last char is entered ---------------*/
  169.     r++;
  170.     msg_line("Entry without strip and exit on fill");
  171.     wn_plst(  2, r, "Enter your phone # : ", wnp );
  172.     my_get_phone(phone2, 14, G_EXIT_ON_FILL, wnp);
  173.     wn_plst( 50, r, phone2, wnp );
  174.  
  175. /*----------------- display 5 chars and strip the entire mask --------------*/
  176.     r++;
  177.     msg_line("Entry with strip and scrolling, but without arrow chars");
  178.     wn_plst(  2, r, "Enter your phone # : ", wnp );
  179.     my_get_phone(phone3, 5, G_STRIP, wnp);
  180.     wn_plst( 50, r, phone3, wnp );
  181.  
  182. #ifdef __cplusplus
  183.     set_validation_func( (int (*)(...)) time_vld );
  184. #else
  185.     set_validation_func( date_vld );
  186. #endif
  187.  
  188. /*--------------- display full width, strip the entire mask,----------------*/
  189. /*--------------- use the validation hook to validate the date -------------*/
  190. /*--------------- and exit when the last char is entered -------------------*/
  191.     r += 2;
  192.     msg_line("Entry with strip, validation, and exit on fill");
  193.     wn_plst(  2, r, "Enter today's date: ", wnp );
  194.     my_get_date(date1, 8, G_STRIP | G_VALIDATE | G_EXIT_ON_FILL, 1, wnp);
  195.     wn_plst( 50, r, date1, wnp );
  196.  
  197. /*------------------- display full width, don't strip mask, ----------------*/
  198. /*------------------- get the input right-to-left, and ---------------------*/
  199. /*------------------- exit when the last char is entered -------------------*/
  200.     r++;
  201.     msg_line("Right-to-left entry without strip and with exit on fill");
  202.     wn_plst(  2, r, "Enter today's date: ", wnp );
  203.     my_get_date(date2, 8, G_RIGHT_TO_LEFT | G_EXIT_ON_FILL, 0, wnp);
  204.     wn_plst( 50, r, date2, wnp );
  205.  
  206. /*----------------- display 5 chars, strip the entire mask, ----------------*/
  207. /*----------------- and display with arrows --------------------------------*/
  208.     r++;
  209.     msg_line("Entry with strip, scrolling, and arrow characters");
  210.     wn_plst(  2, r, "Enter today's date:  ", wnp );
  211.     my_get_date(date3, 2, G_STRIP | G_ARROW, 1, wnp);
  212.     wn_plst( 50, r, date3, wnp );
  213.  
  214. #ifdef __cplusplus
  215.     set_validation_func( (int (*)(...)) time_vld );
  216. #else
  217.     set_validation_func( time_vld );
  218. #endif
  219.  
  220. /*--------------- display full width and strip to the end ------------------*/
  221. /*--------------- geting the input right-to-left ---------------------------*/
  222.     r += 2;
  223.     msg_line("Right-to-left entry with strip to end");
  224.     wn_plst(  2, r, "Enter current time: ", wnp );
  225.     my_get_time(time1, 5, G_STRIP_END | G_RIGHT_TO_LEFT, wnp);
  226.     wn_plst( 50, r, time1, wnp );
  227.  
  228. /*------------- display full width, don't strip mask -----------------------*/
  229. /*------------- and use the validation hook to validate the time -----------*/
  230.     r++;
  231.     msg_line("Entry without strip and with validation");
  232.     wn_plst(  2, r, "Enter current time: ", wnp );
  233.     my_get_time(time2, 5, G_VALIDATE, wnp);
  234.     wn_plst( 50, r, time2, wnp );
  235.  
  236. /*----------------- display 5 chars and strip the entire mask, -------------*/
  237. /*----------------- displaying arrows for scrolling ------------------------*/
  238.     r++;
  239.     msg_line("Entry with strip, scrolling, and arrow characters");
  240.     wn_plst(  2, r, "Enter current time:  ", wnp );
  241.     my_get_time(time3, 2, G_STRIP | G_ARROW, wnp);
  242.     wn_plst( 50, r, time3, wnp );
  243.     set_validation_func( NULL );
  244.  
  245.     set_gets_hook(NULL);
  246.  
  247.     msg_line("Demo complete, press any key to quit!");
  248.     wait_event();
  249.     wn_destroy(wnp);
  250.     end_video();
  251.     return(0);
  252. }
  253. /*** end of main ***/
  254.  
  255. /*************/
  256. /* ~msg_line */
  257. /*           ****************************************************************/
  258. /* Display the message at the last line on the screen.                      */
  259. /****************************************************************************/
  260. void msg_line( char *msg )
  261. {
  262.     push(Main_wn.att);
  263.     push(Main_wn.inside);
  264.     Main_wn.inside = OFF;
  265.     wn_color(BLACK, LIGHTGRAY, &Main_wn);
  266.     mv_cs(0, 24, &Main_wn);
  267.     wn_claol(&Main_wn);
  268.     wn_plst(CENTERED, 24, msg, &Main_wn);
  269.     pop(Main_wn.inside);
  270.     pop(Main_wn.att);
  271. }
  272. /*** end of message_line ***/
  273.  
  274. /************/
  275. /* ~my_gets */
  276. /*          *****************************************************************/
  277. /* This routine just sets up the mask and template strings and calls the    */
  278. /* wn_gets_ll function.                                                     */
  279. /****************************************************************************/
  280. int my_gets(char *s, int max_len, int disp_width, int flags, int clear, WINDOW *wnp)
  281. {
  282.     char mask[81], template[81];
  283.  
  284.     if( clear )
  285.         setmem(s,max_len,0);
  286.     setmem(mask,max_len,'_'), mask[max_len] = '\0';
  287.     setmem(template,max_len,'*'), template[max_len] = '\0';
  288.     return(wn_gets_ll(s, mask, template, swap_nibbles(wnp->att),
  289.         flags, disp_width, wnp));
  290. }
  291. /*** end of my_gets ***/
  292.  
  293. /*****************/
  294. /* ~my_get_phone */
  295. /*               ************************************************************/
  296. /* This routine just sets up the mask and template strings and calls the    */
  297. /* wn_gets_ll function.                                                     */
  298. /****************************************************************************/
  299. int my_get_phone(char *s, int disp_width, int flags, WINDOW *wnp)
  300. {
  301.     char *mask, *template;
  302.  
  303.     setmem(s,15,0);
  304.     mask     = "(___)-___-____";
  305.     template = " ###  ### ####";
  306.     return(wn_gets_ll(s, mask, template, swap_nibbles(wnp->att),
  307.         flags, disp_width, wnp));
  308. }
  309. /*** end of my_get_phone ***/
  310.  
  311. /****************/
  312. /* ~my_get_date */
  313. /*              *************************************************************/
  314. /* This routine just sets up the mask and template strings and calls the    */
  315. /* wn_gets_ll function.                                                     */
  316. /****************************************************************************/
  317. int my_get_date(char *s, int disp_width, int flags, int clear, WINDOW *wnp)
  318. {
  319.     char *mask, *template;
  320.  
  321.     if( clear )
  322.         setmem(s,9,0);
  323.     mask     = "__/__/__";
  324.     template = "## ## ##";
  325.     return(wn_gets_ll(s, mask, template, swap_nibbles(wnp->att),
  326.         flags, disp_width, wnp));
  327. }
  328. /*** end of my_get_date ***/
  329.  
  330. /****************/
  331. /* ~my_get_time */
  332. /*              *************************************************************/
  333. /* This routine just sets up the mask and template strings and calls the    */
  334. /* wn_gets_ll function.                                                     */
  335. /****************************************************************************/
  336. int my_get_time(char *s, int disp_width, int flags, WINDOW *wnp)
  337. {
  338.     char *mask, *template;
  339.  
  340.     setmem(s,6,0);
  341.     mask     = "__:__";
  342.     template = "## ##";
  343.     return(wn_gets_ll(s, mask, template, swap_nibbles(wnp->att),
  344.         flags, disp_width, wnp));
  345. }
  346. /*** end of my_get_time ***/
  347.  
  348. /*************/
  349. /* ~date_vld */
  350. /*           ****************************************************************/
  351. /* This is the validation function for dates.  The format is mm/dd/yy.      */
  352. /****************************************************************************/
  353. int date_vld(char *w, char *t, char *m, int pos, int max_pos, WINDOW *wnp )
  354. {
  355.     int ok = 1;
  356.  
  357.     if( !range('0',w[0],'1') && (w[0] != m[0]) )
  358.             ok = 0, w[0] = m[0];
  359.     if( w[0] == '0' )
  360.     {
  361.         if( !range('1',w[1],'9') && (w[1] != m[1]) )
  362.                 ok = 0, w[1] = m[1];
  363.     }
  364.     else if( w[0] == '1' )
  365.     {
  366.         if( !range('0',w[1],'2') && (w[1] != m[1]) )
  367.                 ok = 0, w[1] = m[1];
  368.     }
  369.  
  370.     if( !range('0',w[3],'3') && (w[3] != m[3]) )
  371.             ok = 0, w[3] = m[3];;
  372.     if( w[3] == '3' )
  373.     {
  374.         if( !range('0',w[4],'1') && (w[4] != m[4]) )
  375.                 ok = 0, w[4] = m[4];;
  376.     }else{
  377.         if( !range('0',w[4],'9') && (w[4] != m[4]) )
  378.                 ok = 0, w[4] = m[4];;
  379.     }
  380.     if( !range('0',w[6],'9') && (w[6] != m[6]) )
  381.             ok = 0, w[6] = m[6];;
  382.     if( !range('0',w[7],'9') && (w[7] != m[7]) )
  383.             ok = 0, w[7] = m[7];;
  384.  
  385.     if( !ok )
  386.     {
  387.         tone(1024,20);
  388.         wn_plst( CENTERED, 18, "Invalid Date - Hit any key to continue", wnp );
  389.         wait_event();
  390.         wn_claol(wnp);
  391.         return(0);
  392.     }
  393.     return(1);
  394. }
  395. /*** end of date_vld ***/
  396.  
  397. /*************/
  398. /* ~time_vld */
  399. /*           ****************************************************************/
  400. /* This is the validation function for time.  The format is hh:mm.          */
  401. /****************************************************************************/
  402. int time_vld(char *w, char *t, char *m, int pos, int max_pos, WINDOW *wnp )
  403. {
  404.     int ok = 1;
  405.  
  406.     if( !range('0',w[0],'2') && (w[0] != m[0]) )
  407.             ok = 0, w[0] = m[0];
  408.  
  409.     if( w[0] == '2' )
  410.     {
  411.         if( !range('0',w[1],'3') && (w[1] != m[1]) )
  412.                 ok = 0, w[1] = m[1];
  413.     }else{
  414.         if( !range('0',w[1],'9') && (w[1] != m[1]) )
  415.                 ok = 0, w[1] = m[1];
  416.     }
  417.     if( !range('0',w[3],'5') && (w[3] != m[3]) )
  418.             ok = 0, w[3] = m[3];;
  419.     if( !range('0',w[4],'9') && (w[4] != m[4]) )
  420.             ok = 0, w[4] = m[4];;
  421.  
  422.     if( !ok )
  423.     {
  424.         tone(1024,20);
  425.         wn_plst( CENTERED, 18, "Invalid Time - Hit any key to continue", wnp );
  426.         wait_event();
  427.         wn_claol(wnp);
  428.         return(0);
  429.     }
  430.     return(1);
  431. }
  432. /*** end of time_vld ***/
  433.  
  434. /***************/
  435. /* ~super_hook */
  436. /*             **************************************************************/
  437. /* This is the overall wn_gets_ll "super" hook.  We will use it for all     */
  438. /* entries in this demo to show the parameters on the fly!  Note that this  */
  439. /* function is only called when you call set_gets_hook(super_hook).  When   */
  440. /* you want the wn_gets_ll function quit calling this function, just use    */
  441. /* set_gets_hook(NULL).                                                     */
  442. /* Notice that the display here at the bottom of the window stays "one      */
  443. /* behind" the regular display above!  This is because this function is     */
  444. /* called from withing wn_gets_ll BEFORE the key is processed.  What this   */
  445. /* means to you is that you can take the key in the event and set it to     */
  446. /* something you like better!!!  We will take the F1 key and translate it   */
  447. /* into a CTRL-Y, commonly used to clear a string.                          */
  448. /****************************************************************************/
  449. int super_hook( char *text, char *template, char *mask, int pos,
  450.                                 int max_pos, int p_flag, EVENT *eventp, WINDOW *wnp )
  451. {
  452.     switch(p_flag)
  453.     {
  454.         case H_ENTRY:                                                                /* wn_gets_ll start                    */
  455.             break;
  456.  
  457.         case H_EXIT:                                                                /* wn_gets_ll end                        */
  458.             break;
  459.  
  460.         case H_PROCESS:                                                            /* each wn_gets_ll key!            */
  461.             if (eventp->key == KEY_F1)                                /* make F1 clear the entry!    */
  462.                 eventp->key = KEY_CTRL_Y;
  463.             break;
  464.     }
  465.     push(wnp->csr_x);                                                            /* we need to save the att  */
  466.     push(wnp->csr_y);                                                            /* and window cursor, since */
  467.     push(wnp->att);                                                                /* we are being called from */
  468.     wn_color(YELLOW, BLUE, wnp);                                    /* within wn_gets_ll!       */
  469.     mv_cs(10, 19, wnp);
  470.     wn_printf(wnp, "    Text: %-25s     Pos: %02d", text, pos);
  471.     wn_cleol(wnp);
  472.     mv_cs(10, 20, wnp);
  473.     wn_printf(wnp, "    Mask: %-25s Max Pos: %02d", mask, max_pos);
  474.     wn_cleol(wnp);
  475.     mv_cs(10, 21, wnp);
  476.     wn_printf(wnp, "Template: %-25s     Key: 0x%04X", template, eventp->key);
  477.     wn_cleol(wnp);
  478.     pop(wnp->att);
  479.     pop(wnp->csr_y);
  480.     pop(wnp->csr_x);
  481.     return(1);
  482. }
  483. /*** end of super_hook ***/
  484.  
  485.  
  486. /**** END OF FILE ****/
  487.